package com.team24.hotel.service.impl;

import com.team24.hotel.domain.ParentPermission;
import com.team24.hotel.domain.Permission;
import com.team24.hotel.mapper.ParentPermissionMapper;
import com.team24.hotel.mapper.PermissionMapper;
import com.team24.hotel.service.ParentPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParentPermissionServiceImpl implements ParentPermissionService {

    @Autowired
    private ParentPermissionMapper parentPermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<ParentPermission> findAll() {
        return parentPermissionMapper.selectAll();
    }

    @Override
    public ParentPermission findByPrimaryKey(Integer id) {
        return parentPermissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int saveOrUpdate(ParentPermission parentPermission) {
        if(parentPermission.getParentPermissionId()!=null){
            return parentPermissionMapper.updateByPrimaryKeySelective(parentPermission);
        }else {
            return parentPermissionMapper.insert(parentPermission);
        }
    }

    @Override
    public int deleteParentPermission(Integer id) {
        List<Permission> permissions=permissionMapper.selectAllByParentPermissionId(id);
        for (Permission permission: permissions
        ) {
            permissionMapper.updateParentPermissionId(permission.getPermissionId());
        }
        return parentPermissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ParentPermission> findByUserId(String userId) {
        return parentPermissionMapper.selectAllByUserId(userId);
    }
}
